<?php

class UserController extends Controller
{
	public function actionDetail()
	{
		//Get Id
		$id = Yii::app()->request->getParam('id');
		$error = '';
		$msg = '';
		//Mode update
		if (is_numeric($id))
		{
			$model = User::model()->findByPk($id);
			if ($model == null) 
			{
				$error = '<li>User['.$id.'] not found. Please add new.</li>';
				$model = new User;
			}
		}
		//Add New
		else
		{
			$model=new User;
		}
		if(isset($_POST['User']))
		{
			$model->attributes=$_POST['User'];
			//$error = $this->validate($model);
			if($error == '')
			{
				$isCreation = false;
				if ($model->password2 != null) $model->password = md5($model->password2);
				if ($model->id <= 0) {
					$isCreation = true;
					$model->createddate = new CDbExpression('NOW()');
				}
				$model->modifieddate = new CDbExpression('NOW()');
				//Save success
				if ($model->save()) 
				{
					$msg = '<li>User has been saved success.</li>';
					if ($isCreation)
					{
						Yii::app()->session['success_msg'] = $msg;
						$this->redirect(Yii::app()->createUrl('admin/user/list'));
					}
				}
				else
				{
					foreach($model->getErrors() as $errs)
					{
						foreach($errs as $err)
						{
							$error	.= '<li>' . $err . '</li>';
						}	
					}
				}
			}
		}
		$this->render('detail',array('model'=>$model, 'error'=>$error, 'msg'=>$msg));
	}

	public function actionDelete()
	{
		//Get Id
		$id = Yii::app()->request->getParam('id');
		$model = User::model()->findByPk($id);
		$model->active = 0;
		$model->save();
		foreach($model->getErrors() as $errs)
					{
						foreach($errs as $err)
						{
							echo '<li>' . $err . '</li>';
						}	
					}
	}
	public function actionIndex()
	{
		$this->actionList();
	}
	public function actionList()
	{
		$msg = '';
		if (isset(Yii::app()->session['success_msg']))
		{
			$msg = Yii::app()->session['success_msg'];
			unset(Yii::app()->session['success_msg']);
		}
		$criterias = new CDbCriteria(array(
			'order'=>'username ASC',
			//'condition'=>'active=1'
		));
		$dataProvider = new CActiveDataProvider('User', array('criteria'=>$criterias));
		$this->render('list', array('dataProvider'=>$dataProvider, 'msg'=>$msg));
	}

	public function accessRules()
	{
		$loggedInUser = Yii::app()->user->__isset('loggedInUser') ? Yii::app()->user->loggedInUser : null;
		return array(
			array(
				'deny',
				'actions'=>array('detail', 'delete', 'list', 'index'),
				'users'=>array(($loggedInUser == null || !$loggedInUser->isadmin) ? '*' : '?')
			),
		);
	}
	// Uncomment the following methods and override them if needed
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'accessControl'
		);
	}
	/*
	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}